590. N 叉树的后序遍历
为保证权益,题目请参考 590. N 叉树的后序遍历(From LeetCode).
解决方案1
Python
python
# 590. N 叉树的后序遍历
# https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/
from typing import List
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
class Solution:
def postorder(self, root: 'Node') -> List[int]:
ans = []
def dfs(root: Node, ans):
if root:
for chi in root.children:
dfs(chi, ans)
ans.append(root.val)
dfs(root, ans)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26